package io.yuncheng.modules.sys.service.impl;

import io.yuncheng.common.config.DictConfig;
import io.yuncheng.common.utils.SysDictConstant;
import io.yuncheng.modules.sys.entity.SubCascadePlatformVo;
import io.yuncheng.modules.sys.entity.SysDictEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.yuncheng.common.utils.PageUtils;
import io.yuncheng.common.utils.Query;

import io.yuncheng.modules.sys.dao.SubCascadePlatformDao;
import io.yuncheng.modules.sys.entity.SubCascadePlatformEntity;
import io.yuncheng.modules.sys.service.SubCascadePlatformService;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;


@Service("subCascadePlatformService")
public class SubCascadePlatformServiceImpl extends ServiceImpl<SubCascadePlatformDao, SubCascadePlatformEntity> implements SubCascadePlatformService {

    @Resource
    private DictConfig dictConfig;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Object cascadeType = params.get("cascadeType");
        IPage<SubCascadePlatformEntity> page = this.page(
                new Query<SubCascadePlatformEntity>().getPage(params),
                new QueryWrapper<SubCascadePlatformEntity>().eq(cascadeType!=null,"cascade_type",cascadeType)
        );
        List<SubCascadePlatformEntity> records = page.getRecords();
        List<SubCascadePlatformVo> voList = new ArrayList<>();
        for (SubCascadePlatformEntity record : records) {
            SubCascadePlatformVo subCascadePlatformVo = SubCascadePlatformVo.buildVo(record);
            SysDictEntity sysDict = dictConfig.getSysDict(SysDictConstant.CASCADE_STATUS, record.getCascadeStatus() + "");
            if(sysDict!=null){
                subCascadePlatformVo.setCascadeStatusName(sysDict.getValue());
            }
            voList.add(subCascadePlatformVo);
        }
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(voList);
        return pageUtils;
    }
    @Override
    public  SubCascadePlatformEntity queryByCascadeIdAndType(Long cascade,Integer type){
        QueryWrapper<SubCascadePlatformEntity> queryWrapper = new QueryWrapper<>();
        SubCascadePlatformEntity entity = new SubCascadePlatformEntity();
        entity.setCascadeId(cascade);
        entity.setCascadeType(type);
        queryWrapper.setEntity(entity);
        List<SubCascadePlatformEntity> list = list(queryWrapper);
        if(ObjectUtils.isEmpty(list)){
            return list.get(0);
        }
        return null;
    }

}
